<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @addT="add1" :list="list"></TodoHeader>
    <TodoMain :list="showlist" @delT="del1"></TodoMain>
    <TodoFooter
      @xuanze="xz"
      :selected="sel"
      @shanchu="sc"
      :changdu="showlist.length"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import './styles/base.css'
import './styles/index.css'

import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'

export default {
  data() {
    return {
      list:  JSON.parse(localStorage.getItem('todolist'))||[],
      // list: [
      //   { id: 100, name: '吃饭', isDone: true },
      //   { id: 101, name: '睡觉', isDone: false },
      //   { id: 102, name: '打豆豆', isDone: true },
      // ],
      sel: 'all',
    }
  },
  computed: {
    showlist() {
      if (this.sel === 'all') {
        return this.list
      } else if (this.sel === 'no') {
        return this.list.filter((item) => !item.isDone)
      } else {
        return this.list.filter((item) => item.isDone)
      }
    },
  },
  methods: {
    add1(task) {
      const id = this.list.length ? this.list.at(-1).id + 1 : 100
      this.list.push({
        id,
        name: task,
        isDone: false,
      })
    },
    del1(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },
    xz(xz1) {
      this.sel = xz1
    },
    sc() {
      this.list = this.list.filter((item) => !item.isDone)
    },
  },
  watch: {
    list: {
      deep: true,
      handler(){
        localStorage.setItem('todolist',JSON.stringify(this.list))
      }
    },
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
}
</script>
